import jwt
from typing import Optional
from datetime import datetime, timedelta
from passlib.context import CryptContext
from config import SECRET_KEY

ALGORITHM = "HS256"
EXPIRATION_TIME = timedelta(minutes=30)
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")


def create_jwt_token(data: dict) -> str:
    data["exp"] = datetime.utcnow() + EXPIRATION_TIME
    return jwt.encode(data, SECRET_KEY, algorithm=ALGORITHM)


def verify_jwt_token(token: str) -> Optional[dict]:
    try:
        return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
    except jwt.PyJWTError:
        return None
